{/* Copyright 2025 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License. */}

import {Layout} from '../../src/Layout';
export default Layout;
import {FunctionAPI} from '../../src/FunctionAPI';
import docs from 'docs:@react-aria/i18n';

export const section = 'Utilities';
export const description = 'Provides locale-sensitive string matching for filtering and searching.';

# useFilter

<PageDescription>{docs.exports.useFilter.description}</PageDescription>

## Introduction

`useFilter` provides functions for filtering or searching based on substring matches. The builtin JavaScript
string methods `startsWith`, `endsWith`, and `includes` could be used for this, but do not implement locale
sensitive matching. `useFilter` provides options to allow ignoring case, diacritics, and Unicode normalization forms,
which are implemented according to locale-specific rules. It automatically uses the current locale set by the application,
either via the default browser language or via the [I18nProvider](I18nProvider).

## Example

The following example implements a filterable list using a `contains` matching strategy that ignores both case
and diacritics.

```tsx render
'use client';
import React from 'react';
import {useFilter} from 'react-aria';

function Example() {
  const composers = [
    'Wolfgang Amadeus Mozart',
    'Johann Sebastian Bach',
    'Ludwig van Beethoven',
    'Claude Debussy',
    'George Frideric Handel',
    'Frédéric Chopin',
    'Johannes Brahms',
    'Pyotr Ilyich Tchaikovsky',
    'Antonín Dvořák',
    'Felix Mendelssohn',
    'Béla Bartók',
    'Niccolò Paganini'
  ];

  let {contains} = useFilter({
    sensitivity: 'base'
  });

  let [value, setValue] = React.useState('');
  let matchedComposers = composers.filter(composer => contains(composer, value));

  return (
    <>
      <label htmlFor="search-input">Filter: </label>
      <input type="search" id="search-input" value={value} onChange={e => setValue(e.target.value)} />
      <ul style={{height: 300}}>
        {matchedComposers.map((composer, i) =>
          <li key={i}>{composer}</li>)
        }
      </ul>
    </>
  );
}
```

## API

<FunctionAPI function={docs.exports.useFilter} links={docs.links} />

### Filter

<ClassAPI class={docs.exports.Filter} links={docs.links} />